//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BondHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Instruments/Bond.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Yield/RateHelper.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (Cephei::QL::IQuote^ cleanPrice, Cephei::QL::Instruments::IBond^ bond) : CRateHelper(CBondHelper::typeid)
{
    CQuote^ _CcleanPrice;
    CBond^ _Cbond;
    try
    {
#ifdef HANDLE
        _phBondHelper = NULL;
#endif
        _CcleanPrice = safe_cast<CQuote^> (cleanPrice);
        _CcleanPrice->Lock();
        Handle<QuantLib::Quote>& _cleanPrice = static_cast<Handle<QuantLib::Quote>&> (_CcleanPrice->GetHandle ()); 
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        boost::shared_ptr<QuantLib::Bond>& _bond = static_cast<boost::shared_ptr<QuantLib::Bond>&> (_Cbond->GetShared ()); 
        _ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (new QuantLib::BondHelper ( _cleanPrice,  _bond ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcleanPrice != nullptr) _CcleanPrice->Unlock();
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (boost::shared_ptr<QuantLib::BondHelper>& childNative, Object^ owner) : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	_ppBondHelper = &childNative;
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
}
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (QuantLib::BondHelper& childNative, Object^ owner) : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	_ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (&childNative);
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
    _BondHelperOwner = owner;
    _RateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (CBondHelper^ copy) : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (copy->GetShared());
        _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
    }
}
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (System::Type^ t) : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	if (!t->IsSubclassOf(CBondHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (QuantLib::Handle<QuantLib::BondHelper>& childNative, Object^ owner)  : CRateHelper(CBondHelper::typeid)
{
	_phBondHelper = &childNative;
	_ppBondHelper = &static_cast<boost::shared_ptr<QuantLib::BondHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
    _BondHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (QuantLib::Handle<QuantLib::BondHelper> childNative)  : CRateHelper(CBondHelper::typeid)
{
	_phBondHelper = &childNative;
	_ppBondHelper = &static_cast<boost::shared_ptr<QuantLib::BondHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (QuantLib::BondHelper childNative)  : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	_ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (new QuantLib::BondHelper (childNative));
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
}
#endif

Cephei::QL::Termstructures::Yield::CBondHelper::~CBondHelper ()
{
    if (_ppBondHelper != NULL)
    {
	    delete _ppBondHelper;
        _ppBondHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CBondHelper::!CBondHelper ()
{
    if (_ppBondHelper != NULL)
    {
	    delete _ppBondHelper;
    }
}
QuantLib::BondHelper& Cephei::QL::Termstructures::Yield::CBondHelper::GetReference ()
{
    if (_ppBondHelper == NULL) throw gcnew NativeNullException ();
	return **_ppBondHelper;
}
boost::shared_ptr<QuantLib::BondHelper>& Cephei::QL::Termstructures::Yield::CBondHelper::GetShared ()
{
    if (_ppBondHelper == NULL) throw gcnew NativeNullException ();
	return *_ppBondHelper;
}
QuantLib::BondHelper* Cephei::QL::Termstructures::Yield::CBondHelper::GetPointer ()
{
    if (_ppBondHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppBondHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BondHelper>& Cephei::QL::Termstructures::Yield::CBondHelper::GetHandle ()
{
	if (_phBondHelper == NULL)
	{
		_phBondHelper = new Handle<QuantLib::BondHelper> (*_ppBondHelper);
	}
	return *_phBondHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::CBondHelper::HasNative () 
{
	return (_ppBondHelper != NULL);
}

Cephei::QL::Instruments::IBond^ Cephei::QL::Termstructures::Yield::CBondHelper::Bond::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Bond> _rv = (boost::shared_ptr<QuantLib::Bond>)(*_ppBondHelper)->bond ( );   
        Cephei::QL::Instruments::CBond^ _nrv = gcnew Cephei::QL::Instruments::CBond (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Yield::CBondHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::IBondHelper^ Cephei::QL::Termstructures::Yield::CBondHelper::SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t)
{
    CYieldTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYieldTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YieldTermStructure* _t = static_cast<QuantLib::YieldTermStructure*> (_Ct->GetPointer ()); 
    	(*_ppBondHelper)->setTermStructure ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IBondHelper^ Cephei::QL::Termstructures::Yield::CBondHelper_Factory::Create (Cephei::QL::IQuote^ cleanPrice, Cephei::QL::Instruments::IBond^ bond)
{
    return gcnew CBondHelper ( cleanPrice,  bond);
}
